﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Reflection;
using System.Linq;
public class NewBehaviourScript : MonoBehaviour
{

    // Start is called before the first frame update
    void Start()
    {

        List<int> list1 = new List<int> { 1, 2, 3 };
        List<int> list2 = new List<int> { 1, 2, 3 };
        List<int> list3 = new List<int> { 3, 2, 1 };

        bool result1 = list1.SequenceEqual(list2);
        bool result2 = list1.SequenceEqual(list3);

        Debug.Log($"list1 and list2 are equal: {result1}"); // Output: True
        Debug.Log($"list1 and list3 are equal: {result2}"); // Output: False



        var set1 = new HashSet<int>(list1);
        var set2 = new HashSet<int>(list3);

        Debug.Log(set1.SetEquals(set2));

        IBank bank = new ATMProxy(); // 代理对象
        bank.WithdrawMoney(100);

        var sensor = new TemperatureSensor();
        new Display().Subscribe(sensor);
        sensor.CheckTemperature();


        var chain = new TeamLeader();
        chain.SetNext(new Manager());
        chain.ProcessRequest(2);
        chain.SetNext(new TeamLeader());
        chain.ProcessRequest(10);



        IShape shape = new Cube();
        shape.Accept(new RenderVisitor());


        Composite root = new Composite("Root");
        root.Add(new Leaf("Leaf A"));
        root.Add(new Leaf("Leaf B"));

        Composite comp = new Composite("Composite X");
        comp.Add(new Leaf("Leaf XA"));
        comp.Add(new Leaf("Leaf XB"));

        root.Add(comp);

        root.Display(1);


        ConcreteHandler1 handler1 = new ConcreteHandler1();
        ConcreteHandler2 handler2 = new ConcreteHandler2();
        handler1.SetSuccessor(handler2);

        handler1.HandleRequest(5);
        handler1.HandleRequest(15);



        Context context = new Context();
        AbstractExpression left = new TerminalExpression();
        AbstractExpression right = new TerminalExpression();
        AbstractExpression nonTerminal = new NonTerminalExpression(left, right);
        nonTerminal.Interpret(context);


        test t = new test("111") { value1 = "123" };
        Debug.Log(t.value1);

    }

    // Update is called once per frame
    void Update()
    {

    }
    
    
    
}


public class test
{
    public string value1;


    public test(string value) { 
        value1 = value;
    }


}

// 抽象表达式 
public abstract class AbstractExpression
{
    public abstract void Interpret(Context context);
} 
 
// 终结符表达式 
public class TerminalExpression : AbstractExpression 
{ 
    public override void Interpret(Context context) 
    { 
        Debug.Log("TerminalExpression Interpret"); 
    } 
} 
 
// 非终结符表达式 
public class NonTerminalExpression : AbstractExpression 
{ 
    private AbstractExpression left; 
    private AbstractExpression right; 
 
    public NonTerminalExpression(AbstractExpression left, AbstractExpression right) 
    { 
        this.left  = left; 
        this.right  = right; 
    } 
 
    public override void Interpret(Context context) 
    { 
        left.Interpret(context); 
        right.Interpret(context); 
    } 
} 
 
// 上下文 
public class Context 
{ 
    // 上下文信息 
} 



// 抽象处理者 
public abstract class Handler
{
    protected Handler successor;

    public void SetSuccessor(Handler successor)
    {
        this.successor = successor;
    }

    public abstract void HandleRequest(int request);
} 
 
// 具体处理者1 
public class ConcreteHandler1 : Handler 
{ 
    public override void HandleRequest(int request) 
    { 
        
        if (request >= 0 && request < 10)
        {
            Debug.Log($"ConcreteHandler1 handled request {request}"); 
        }
        else if (successor != null)
        {
            successor.HandleRequest(request);
        } 
    } 
} 
 
// 具体处理者2 
public class ConcreteHandler2 : Handler 
{ 
    public override void HandleRequest(int request) 
    { 
        
        if (request >= 10 && request < 20)
        {
            Debug.Log($"ConcreteHandler2 handled request {request}"); 
        }
        else if (successor != null)
        {
               successor.HandleRequest(request); 
        } 
    } 
} 


// 抽象组件 
public abstract class Component1 
{ 
    protected string name; 
 
    public Component1(string name) 
    { 
        this.name  = name; 
    } 
 
    public abstract void Add(Component1 c); 
    public abstract void Remove(Component1 c); 
    public abstract void Display(int depth); 
} 
 
// 叶子节点 
public class Leaf : Component1 
{ 
    public Leaf(string name) : base(name) 
    { 
    } 
 
    public override void Add(Component1 c) 
    { 
        Debug.Log("Cannot add to a leaf"); 
    } 
 
    public override void Remove(Component1 c) 
    { 
        Debug.Log("Cannot remove from a leaf"); 
    } 
 
    public override void Display(int depth) 
    { 
        Debug.Log(new String('-', depth) + name); 
    } 
} 
 
// 容器节点 
public class Composite : Component1
{ 
    private List<Component1> children = new List<Component1>(); 
 
    public Composite(string name) : base(name) 
    { 
    } 
 
    public override void Add(Component1 c) 
    { 
        children.Add(c); 
    } 
 
    public override void Remove(Component1 c) 
    { 
        children.Remove(c); 
    } 
 
    public override void Display(int depth) 
    { 
        Console.WriteLine(new String('-', depth) + name); 
        foreach (Component1 component in children) 
        { 
            component.Display(depth + 2); 
        } 
    } 
} 


















public interface IBank
{
    void WithdrawMoney(int amount);
}
public class RealBank : IBank {
    public void WithdrawMoney(int amount) => Debug.Log($"银行出款：{amount}");
}
public class ATMProxy : IBank {
    private RealBank _bank = new RealBank();
    public void WithdrawMoney(int amount) {
        if (ValidateUser()) _bank.WithdrawMoney(amount);
    }
    private bool ValidateUser() => true; // 身份验证逻辑
}
public class TemperatureSensor {
    public event Action<int> OnTemperatureChanged;
    public void CheckTemperature() {
        int temp = 12;
        OnTemperatureChanged?.Invoke(temp);
    }
}
public class Display
{
    public void Subscribe(TemperatureSensor sensor)
    {
        sensor.OnTemperatureChanged += temp =>
            Debug.Log($"当前温度：{temp}℃");
    }
}


public abstract class Approver {
    protected Approver _nextApprover;
    public void SetNext(Approver approver) => _nextApprover = approver;
    public abstract void ProcessRequest(int days);
}
public class TeamLeader : Approver {
    public override void ProcessRequest(int days) {
        if (days <= 1) Debug.Log("组长批准");
        else _nextApprover?.ProcessRequest(days);
    }
}
public class Manager : Approver {
    public override void ProcessRequest(int days) {
        if (days <= 3) Debug.Log("经理批准");
        else _nextApprover?.ProcessRequest(days);
    }
}
// 使用：
 public interface IShape {
    void Accept(IShapeVisitor visitor);
}
public class Cube : IShape {
    public void Accept(IShapeVisitor visitor) => visitor.Visit(this);
}
public class Sphere : IShape {
    public void Accept(IShapeVisitor visitor) => visitor.Visit(this);
}

public interface IShapeVisitor {
    void Visit(Cube cube);
    void Visit(Sphere sphere);
}
// 具体访问者
public class RenderVisitor : IShapeVisitor {
    public void Visit(Cube cube) => Debug.Log("渲染立方体");
    public void Visit(Sphere sphere) => Debug.Log("渲染球体");
}